<?php
/* SVN FILE: $Id: author.php 259 2010-04-22 08:11:25Z miha@nahtigal.com $ */
/**
 * Short description for author.php
 *
 * Long description for author.php
 *
 * PHP versions 4 and 5
 *
 * Copyright (c) 2009, Miha Nahtigal
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright (c) 2009, Miha Nahtigal
 * @link          http://www.nahtigal.com/
 * @package       lil
 * @subpackage    lil.models
 * @since         v 1.0
 * @version       $Revision: 259 $
 * @modifiedby    $LastChangedBy: miha@nahtigal.com $
 * @lastmodified  $Date: 2010-04-22 08:11:25 +0000 (Thu, 22 Apr 2010) $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */
/**
 * Author class
 *
 * @uses          LilAppModel
 * @package       lil
 * @subpackage    lil.models
 */
class Author extends LilAppModel {
/**
 * name property
 *
 * @var string 'Author'
 * @access public
 */
	var $name = 'Author';
/**
 * displayField property
 *
 * @var string 'name'
 * @access public
 */
	var $displayField = 'name';
/**
 * hasMany property
 *
 * @var array
 * @access public
 */
	var $hasMany = array(
		'Post' => array(
			'className'  => 'Lil.Post',
			'foreignKey' => 'author_id'
		)
	);
/**
 * validate property
 *
 * @var array
 * @access public
 */
	var $validate = array(
		'name'  => array('rule' => array('minLength', '1'), 'required' => true),
		'email' => array('rule' => 'email', 'required' => false),
		'repeat_pass' =>  array(
			'rule'       => 'testRepeatPassword',
			'required'   => false,
		),
		'old_pass' => array(
			'rule'       => 'testOldPassword',
			'required'   => false,
		),
	);
/**
 * testRepeatPassword method
 *
 * @param mixed $check
 * @access private
 * @return void
 */
	function testRepeatPassword($check) {
		$value = array_values($check);
		$value = $value[0];
		if (empty($this->data['Author']['passwd']) || 
			(Security::hash('', null, true) == $this->data['Author']['passwd'])) {
			return true;
		}
		return isset($this->data['Author']['passwd']) &&
			$this->data['Author']['passwd'] == $value;
	}
/**
 * testOldPassword method
 *
 * @param mixed $check
 * @access private
 * @return void
 */
	function testOldPassword($check) {
		$value = array_values($check);
		$value = $value[0];
		// return true on empty password (do nothing on empty password)
		if (empty($this->data['Author']['passwd']) || 
			(Security::hash('', null, true) == $this->data['Author']['passwd'])) {
			return true;
		}
		
		if (isset($this->data['Author']['id'])) {
			return $this->hasAny(
				array(
					$this->primaryKey => $this->data['Author'][$this->primaryKey],
					'Author.passwd'   => Security::hash($value, null, true)
				)
			);
		}
		return false;
	}
/**
 * beforeSave callback
 *
 * @access public
 * @return boolean
 */
	function beforeSave() {
		if (isset($this->data['Author']['passwd'])) {
			if (
				empty($this->data['Author']['passwd']) ||
				($this->data['Author']['passwd'] == Security::hash('', null, true))
			) {
				unset($this->data['Author']['passwd']);
			} else {
				$this->data['Author']['passwd'] = Security::hash($this->data['Author']['passwd'], null, true);
			}
		}
		
		return true;
	}
/**
 * __construct method
 *
 * @param mixed $id
 * @param mixed $table
 * @param mixed $ds
 * @access private
 * @return void
 */
	function __construct($id = false, $table = null, $ds = null) {
		$this->displayField = Configure::read('Lil.authorDisplayField');
		$this->useTable = Configure::read('Lil.authorsTable');
		
		if (Configure::read('Lil.authorDisplayField') != 'name') {
			$this->validate[Configure::read('Lil.authorDisplayField')] = $this->validate['name'];
			unset($this->validate['name']);
		}
		parent::__construct($id, $table, $ds);
	}
}
?>